package com.ymatou.iapi.liveseller.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.liveseller.parameter.CheckProductToLiveBean;
import com.ymatou.iapi.liveseller.service.CheckProductToLiveCall;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.utils.TestDataManager;
/**
*检测商品是否可以加入直播间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CheckProductToLive{
	private static CheckProductToLiveBean checkproducttoliveBean;
	private static CheckProductToLiveCall checkproducttoliveCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("检测商品是否可以加入直播间");
	}

	@Before
	public void caseUp() {
		checkproducttoliveBean=new CheckProductToLiveBean();
		checkproducttoliveCall=new CheckProductToLiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(checkproducttoliveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CheckProductToLive_001() {
		Logger.start(true,"检测商品是否可以加入直播间成功");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			
			checkproducttoliveBean.setLiveId(createlive.getId());
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(uinfo.getUserId());
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("200", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("可以", checkproducttoliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckProductToLive_002() {
		Logger.start(true,"商品不存在失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
//			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
//			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =TestDataManager.getRandomUUID().toString();
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			
			checkproducttoliveBean.setLiveId(createlive.getId());
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(uinfo.getUserId());
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("201", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("不存在", checkproducttoliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckProductToLive_003() {
		Logger.start(true,"商品已下架失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
//			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			ProductManagerCallServiceV2.batchSetProductOffSale(Arrays.asList(pid), uinfo.getUserId());
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			
			checkproducttoliveBean.setLiveId(createlive.getId());
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(uinfo.getUserId());
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("201", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("下架", checkproducttoliveCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//测试时候需要修改disconf bizconfig maxLiveProductCount=1
	public void Tc_CheckProductToLive_004() {
		Logger.start(true,"直播间商品到达上限失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			AddSellerProductRequest addpro2 = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid2 =ProductManagerCallServiceV3.AddSellerProduct(addpro2);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), uinfo.getUserId(), Arrays.asList(pid));
			checkproducttoliveBean.setLiveId(createlive.getId());
			checkproducttoliveBean.setProductId(pid2);
			checkproducttoliveBean.setUserId(uinfo.getUserId());
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("201", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("上限", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckProductToLive_005() {
		Logger.start(true,"不输入userid失败");
		try {
			int liveid=1;
			int userid=1;
			String pid =TestDataManager.getRandomUUID().toString();
			
			checkproducttoliveBean.setLiveId(liveid);
			checkproducttoliveBean.setProductId(pid);
//			checkproducttoliveBean.setUserId(uinfo.getUserId());
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("400", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("买手编号", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckProductToLive_006() {
		Logger.start(true,"不输入liveId失败");
		try {
			int liveid=1;
			int userid=1;
			String pid =TestDataManager.getRandomUUID().toString();
			
//			checkproducttoliveBean.setLiveId(liveid);
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(userid);
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("400", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("直播编号", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_CheckProductToLive_007() {
		Logger.start(true,"不输入productId失败");
		try {
			int liveid=1;
			int userid=1;
			String pid =TestDataManager.getRandomUUID().toString();
			
			checkproducttoliveBean.setLiveId(liveid);
//			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(userid);
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("400", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("商品编号", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckProductToLive_008() {
		Logger.start(true,"直播不存在失败");
		try {
			int liveid=1;
			int userid=1;
			String pid =TestDataManager.getRandomUUID().toString();
			
			checkproducttoliveBean.setLiveId(liveid);
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(userid);
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("201", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("直播不存在", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckProductToLive_009() {
		Logger.start(true,"直播和商品不匹配失败");
		try {
			
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			int liveid=createlive.getId();
			int userid=1;
			String pid =TestDataManager.getRandomUUID().toString();
			
			checkproducttoliveBean.setLiveId(liveid);
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(userid);
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("201", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("无权操作", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CheckProductToLive_010() {
		Logger.start(true,"直播中已存在此商品失败");
		try {
			List<Userinfo> list = new JUserWapper().select1KSeller();
			Userinfo uinfo = list.get(0);
			//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(uinfo.getUserId());
			AddSellerProductRequest addpro = ProductManagerCallServiceV3.initAddSellerProductRequest(uinfo.getUserId());
			String pid =ProductManagerCallServiceV3.AddSellerProduct(addpro);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(uinfo));
			LiveRoomCallService.AddProductsToLive(createlive.getId(), uinfo.getUserId(), Arrays.asList(pid));
			checkproducttoliveBean.setLiveId(createlive.getId());
			checkproducttoliveBean.setProductId(pid);
			checkproducttoliveBean.setUserId(uinfo.getUserId());
			checkproducttoliveCall.setData(checkproducttoliveBean);
			checkproducttoliveCall.callService();			
			Logger.verifyEquals("201", checkproducttoliveCall.getString("Code"), "验证返回");
			Logger.verifyIsContains("已存在", checkproducttoliveCall.getString("Msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}